package Project2.junitframe.framework;

import Project2.junitframe.annotation.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class RunMyTest {
	public static void Run(Class Clazz){
		Class<Object> a = Clazz;
		
		Method[] methods = a.getDeclaredMethods();
		
		List<Method> tests = new ArrayList<>();
		List<Method> before = new ArrayList<>();
		List<Method> afters = new ArrayList<>();
		List<Method> beforeClass = new ArrayList<>();
		List<Method> afterClass = new ArrayList<>();
		
		// 处理 注解类型 与 方法
		for (Method method : methods){
			Annotation[] annotations = method.getAnnotations();
			for (Annotation annotation : annotations){
				if (annotation instanceof MyTest){
					tests.add(method);
				} else if (annotation instanceof MyBefore){
					before.add(method);
				} else if (annotation instanceof MyAfter){
					afters.add(method);
				}else if (annotation instanceof MyBeforeClass){
					beforeClass.add(method);
				} else if (annotation instanceof MyAfterClass){
					afterClass.add(method);
				}
			}
		}
		for (Method bf : beforeClass) { // 执行beforeClass方法
			try {
				bf.invoke(null);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		for (Method method : tests) {
			try {
				Object oa = a.newInstance();
				for (Method bf : before) {  // 执行before方法
					bf.invoke(oa);
				}
				method.invoke(oa); // 执行test方法
				for (Method af : afters) { // 执行after方法
					af.invoke(oa);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		for (Method af : afterClass) { // 执行afterClass方法
			try {
				af.invoke(null);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}
}
